class Solution:
    def parseBoolExpr(self, expression: str) -> bool:
        r, _ = self.parseExpr(expression, 0)
        return r == 't'

    def parseExpr(self, expression: str, start: int):
        # 这一题开头一定是一个逻辑操作符
        i = start
        op = expression[i]
        i += 2 # 跳过没用的 (
        n = len(expression)
        hast, hasf = False, False
        while i < n:
            c = expression[i]
            if c == 't':
                hast = True
                i += 1
            elif c == 'f':
                hasf = True
                i += 1
            elif c in "!&|":
                r, s = self.parseExpr(expression, i)
                if r == 't':
                    hast = True
                else:
                    hasf = True
                i = s
            elif c == ')':
                if op == '!':
                    return ('t' if hasf else 'f', i + 1)
                elif op == '&':
                    return ('f' if hasf else 't', i + 1)
                elif op == '|':
                    return ('t' if hast else 'f', i + 1)
                i += 1
            else:
                i += 1

solu = Solution()
print(solu.parseBoolExpr("|(&(t,f,t),!(t))"))